package question

//1234. 替换子串得到平衡字符串
func balancedString(s string) int {
	l := len(s)
	if l%4 != 0 {
		return -1
	}
	m := make(map[byte]int)
	for i := 0; i < l; i++ {
		m[s[i]]++
	}
	q4 := m['Q'] * 4
	w4 := m['W'] * 4
	e4 := m['E'] * 4
	r4 := m['R'] * 4

	much := 0

	var sum func(a, b int)
	sum = func(a, b int) {
		if a > b {
			much += a - b
		}
	}

	sum(q4, l)
	sum(w4, l)
	sum(e4, l)
	sum(r4, l)

	return much / 4

}
